<?php
/**
 * 栏目管理
 */

namespace backend\controllers;

use common\models\Article;
use common\models\ArticleTo;
use common\models\Meta;
use common\utils\XFile;
use yii;
use common\models\Catalog;

class CatalogController extends CommonController
{
    public function actions()
    {
        return [
            'position' => [
                'class' => 'backend\\actions\\Position',
                'returnUrl' => yii\helpers\Url::current()
            ]
        ];
    }

    public function actionIndex()
    {
        if ($this->get('act') && Yii::$app->request->isAjax) {
            switch ($this->get('act')) {
                case 'getArticle':
                    return $this->getArticle();
                    break;
                default:
                    break;
            }
        } else {
            $dataProvider = new yii\data\ActiveDataProvider([
                'query' => Catalog::find(),
                'pagination' => false
            ]);
            return $this->render('index', ['dataProvider' => $dataProvider]);
        }
    }

    public function actionCreate()
    {
        $model = new Catalog();
        if ($this->get('parent_id')) {
            $model->parent_id = $this->get('parent_id');
        }
        $this->setModel($model);

        $meta = new Meta();
        $articleInfo = ['id' => '', 'title' => ''];
        $viewItem = [];
        $files = XFile::getItem();
        foreach ((array)$files as $item) {
            $viewItem = $viewItem + [$item['basename'] => $item['name']];
        }
        $this->setParams(['meta' => $meta, 'articleInfo' => $articleInfo, 'viewItem' => $viewItem]);

        return $this->save();
    }

    public function actionUpdate($id)
    {
        $model = Catalog::findOne($id);
        $this->setModel($model);

        $meta = Meta::get($model->tableName(), $model->id);
        $articleInfo = ArticleTo::getArticleInfoByType($model->tableName(), $model->id);
        $viewItem = [];
        $files = XFile::getItem();
        foreach ((array)$files as $item) {
            $viewItem = $viewItem + [$item['basename'] => $item['name']];
        }
        $this->setParams(['meta' => $meta, 'articleInfo' => $articleInfo, 'viewItem' => $viewItem]);

        return $this->save();
    }

    public function actionDelete()
    {
        $this->setModel(new Catalog());
        $this->delete();
    }

    public function findModel($id)
    {
        if (($model = Catalog::findOne($id)) !== null) {
            return $model;
        } else {
            $this->tips('该记录已经不存在！');
        }
    }

    /**
     * @param \common\models\Catalog $model
     */
    protected function savedHandle($model)
    {
        Meta::log($model->tableName(), $model->id);
        if ($model->type == 2) {
            $article_id = $this->post('article_id');
            if ($article_id) {
                ArticleTo::log($article_id, $model->tableName(), $model->id);
            }
        } else {
            ArticleTo::del($model->tableName(), $model->id);
        }
    }

    private function getArticle()
    {
        $key = $this->get('key');
        $query = Article::find()->orderBy(['id' => SORT_DESC]);
        if ($key) {
            $query->andWhere(['like', 'title', $key]);
        }
        $dataProvider = new yii\data\ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 15,
            ]
        ]);
        return $this->renderAjax('get-article', ['dataProvider' => $dataProvider]);
    }
}